package com.zzy.redis;



import com.zzy.util.UtilProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.Serializable;


/**
 * redis 配置
 *  有问题  保留
 * @author
 */

public class RedisUtils implements Serializable {


    /**
     * 日志记录
     */
    private static final Log LOG = LogFactory.getLog(RedisUtils.class);

    /**
     * redis 连接池
     */
    private static JedisPool pool;

    //@Value(value = "${redis.host}")//对静态的属性无效

    private static String host;
    private static Integer port;
    private static Integer timeout;
    private static String password;
    private static Integer maxIdle;
    private static Integer minIdle;
    private static Integer maxTotal;
    private static Integer maxWaitMillis;

    static {

       /* host = UtilProperties.getConfig("redis.host");
        port  = UtilProperties.getConfigInteger("redis.port");
        timeout = UtilProperties.getConfigInteger("redis.timeout") ;
        password  = UtilProperties.getConfig("redis.password");
        maxIdle = UtilProperties.getConfigInteger("redis.maxIdle") ;
        minIdle = UtilProperties.getConfigInteger("redis.minIdle") ;
        maxTotal = UtilProperties.getConfigInteger("redis.maxTotal") ;
        maxWaitMillis = UtilProperties.getConfigInteger("redis.maxWaitMillis") ;
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(maxIdle); // # 最大空闲连接数（默认为0，该值只有为正数才有作用）
        poolConfig.setMinIdle(minIdle);// # 最小空闲连接数（默认为0，该值只有为正数才有作用）
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);// # 从连接池中获取连接最大等待时间（默认为-1，单位为毫秒，负数表示无限）
        pool = new JedisPool(poolConfig,host, port, timeout,password);*/
    }




    /**
     * 获取jedis
     *
     * @return jedis
     */
    public Jedis getResource() {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
        } catch (Exception e) {
            LOG.info("can't get the redis resource");
        }
        return jedis;
    }

    /**
     * 关闭连接
     *
     * @param jedis j
     */
    public void disconnect(Jedis jedis) {
        jedis.disconnect();
    }

    /**
     * 将jedis 返还连接池
     *
     * @param jedis j
     */
    public void returnResource(Jedis jedis) {
        if (null != jedis) {
            try {
                pool.returnResource(jedis);
            } catch (Exception e) {
                LOG.info("can't return jedis to jedisPool");
            }
        }
    }

    /**
     * 无法返还jedispool，释放jedis客户端对象
     *
     * @param jedis j
     */
    public void brokenResource(Jedis jedis) {
        if (jedis != null) {
            try {
                pool.returnBrokenResource(jedis);
            } catch (Exception e) {
                LOG.info("can't release jedis Object");
            }
        }
    }
}

